home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: New printf - like function, is it possible?
- Date: 2 Apr 1996 21:37:25 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4jsrpl$s9m@solutions.solon.com>
- References: <4eqb3a$2r5@pan.otol.fi> <311240d2.593861@news.demon.co.uk> <3161918C.72D4@halcyon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <3161918C.72D4@halcyon.com>,
- Glen Parker <glenebob@halcyon.com> wrote:
- >Try this code:
-
- >char* Text;
-
- >Text = (char*)malloc(6); //get memory
- >sprintf(Text, "Hello"); //and stuff it
- > //with formatted text
-
- This demonstrates why crossposts between clc and clc++ are a *bad* idea.
- The first code line here has a superfluous cast; all three comments are
- syntax errors in C.
-
-
- >MessageBox(GetFocus(), Text, "Debug", 0);
- >free((void*)Text); //and free it again
-
- Once again, superfluous cast and syntax error.
-
- >here is the documentation I got from my Borland C++ help file:
-
- (Perfectly reasonable description of sprintf elided.)
-
- >On error, sprintf returns EOF.
-
- Just as a note, printf (in ANSI) returns a negative value (not necessarily
- EOF) on error, and sprintf does not describe any error conditions. This
- behavior is an extension.
-
- I don't think this really answers the question; the key issue here
- is making a variadic function.
-
- Try
-
- #include <stdarg.h>
-
- int
- debug(char *fmt, ...) {
- char buf[2048]; /* arbitrary! beware... */
- va_list ap;
- int ret;
-
- va_start(ap, fmt);
- ret = vsprintf(buf, fmt, ap);
- va_end(ap);
-
- foo(buf); /* your output-plain-string function */
-
- return ret;
- }
-
- The details are left as an exercise for the reader.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-